package modelLayer;
import java.util.ArrayList;


/**
 * Write a description of class DVD here.
 * 
 * @author group 5 
 * @version 2014-11-04
 */
public class DVD{
    private int barcode;
    private String title;
    private String artist;
    private String publicationDate;
    
    
    private ArrayList <Copy> copys;
    
    public DVD (int barcode, String title, String artist, String publicationDate) {
        this.barcode=barcode;
        this.title=title;
        this.artist=artist;
        this.publicationDate=publicationDate;
        copys = new ArrayList<Copy>();
    }
    
    public void addCopy(Copy nCopy){
        copys.add(nCopy);
    }
    /**
     * This method proves that there are copys of the DVD.
     * If there are any copys shows "true" otherwise shows "false".
     * @return boolean
     */
    private boolean isNotEmpty(){
        if(copys.size()!=0)
            return true;
        else return false;
    }
    
    public void deleteCopy(String serialNumber){

      
        if (isNotEmpty()) {
            boolean found= false;
            int index=0;
            while(!found&& index < copys.size()) 
            {
                if(copys.get(index).getSerialNumber().equals(serialNumber)){
                    found=true;
                    copys.remove(index);
                }
                index++;
        }
           if(found==false)
        {
            System.out.println("Enter a valid serial number");
        }
        }
        else
        {
            System.out.println("You don't have any copies");
        }
        
    }

    public void setBarcode(int barcode){
        this.barcode=barcode;
    }
    
    public void setTitle(String title) {
        this.title=title;
    }
    
    public void setArtist(String artist){
        this.artist=artist;
    }
    
    public void setPublicationDate(String publicationDate) {
        this.publicationDate=publicationDate;
    }
    
    public int getBarcode(){
        return barcode;
    }
    
    public String getTitle(){
        return title;
    }
    
    public String getArtist(){
        return artist;
    }
    
    public String getPublicationdate(){
        return publicationDate;
    }
    /**
     * This method finds copy of the DVD by his serial number.
     */
    public Copy findCopyBySerial(String serial){
        if(copys.size() != 0){
            boolean found= false;
            int index=0;
            while(copys.size() > index && !found) {
                if(copys.get(index).getSerialNumber().equals(serial)){
                    found=true;
                    return copys.get(index);
            }
            index++;}
    }
    return null;}
    
    public void showDvd(){
        //int barcode, String title, String artist, String publicationDate
        System.out.println("Barcode: "+ barcode);
        System.out.println("Title: "+title);
        System.out.println("Artist: "+artist);
        System.out.println("Publication date: "+publicationDate);
        System.out.println("------------------------------------------------");
        
    }
    public void showDvdAndItsCopys()
    {
        showDvd();
        showCopysInfo();
    }
    /**
     * This method checks is the copy available.
     * If it is available the loan is made and system shows message:"Loan successfully made!"
     * If it is not available system shows message:"Failed to create a loan!"
     */
    public Copy checkCopy()
    {
        
        boolean found=false;
        int index=0;
        while(!found&&index<copys.size())
        {
            if(copys.get(index).getIsTaken()==false)
            {
            	System.out.println("Loan successfully made!");
                return copys.get(index);
            }
            index++;
        }
        System.out.println("Failed to create a loan!");
        return null;
    }
    public void showCopysInfo(){
        if(copys.size() > 0){
        for(int i=0; i<copys.size(); i++) {
        	System.out.println("Copy #" + (i+1));
            copys.get(i).showCopy();
        	}
        }
        else System.out.println("No Copys for this DVD!");
    }
}

